package com.course.controller;

import com.course.pojo.User;
import com.course.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class UserController {

    private UserService userService;

    @RequestMapping(value = "login",method = RequestMethod.POST)
    @ResponseBody
    @PostMapping("/login")
    public String login(String account,String password){
        User user = userService.login(account, password);
        user.setPassword("");
        if(user == null){
            json.put("code",400);
            json.put("user","");
        }else {
            json.put("code",200);
            json.put("user",user);
        }
        return json.toJSONString();
    }
    @PostMapping("/register")
    public String register(String account, String password){
        try {
            //判断手机号是否存在
            boolean t = userService.is_account(account);
            if(t){
                userService.register(account, password);
                json.put("meg","注册成功");
                json.put("code","200");
            } else{
                json.put("meg","此手机号已存在");
                json.put("code","-1");
            }
        } catch (Exception e) {
            e.printStackTrace();
            json.put("meg","注册失败");
            json.put("code","-1");
        }
        return json.toJSONString();
    }

    /**
     * 发送验证码
     * @param account
     * @return
     */
    /*
    @PostMapping("/push/verification")
    public String pushVerfica(String account){
        HashMap<String, Object> result = null;

        //初始化SDK
        CCPRestSmsSDK restAPI = new CCPRestSmsSDK();
        restAPI.init("app.cloopen.com", "8883");
        restAPI.setAccount("8a216da85cb0540d015cc9cb874b084b", "e9e4a257109e49f28f6ca7970b94b856");
        restAPI.setAppId("8a216da85cb0540d015cc9cb89240852");

        String number = RandomUtil.randomNum(4);
        result = restAPI.sendTemplateSMS("17826853230","1" ,new String[]{number,"5"});

//        System.out.println("SDKTestGetSubAccounts result=" + result);

        if("000000".equals(result.get("statusCode"))){
            //正常返回输出data包体信息（map）
            HashMap<String,Object> data = (HashMap<String, Object>) result.get("data");
            Set<String> keySet = data.keySet();
            for(String key:keySet){
                Object object = data.get(key);
                System.out.println("123456\n"+ key +object);
            }
            Date date = new Date();
            json.put("time", date.getTime());
            json.put("code",200);
            json.put("verification",number);
        }else{
            //异常返回输出错误码和错误信息
            System.out.println("错误码=" + result.get("statusCode") +" 错误信息= "+result.get("statusMsg"));
            json.put("code",400);
            json.put("verification","");
        }

        return json.toJSONString();
    }*/
}
